package ntd.util.frame.data.bo;

import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import lombok.Data;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Data
public class QueryRuleInfo {

    private String name;

    private String val;

    private String queryType;

    public static List<QueryRuleInfo> transform(String rules) {
        if(StrUtil.isBlank(rules)){
            return ListUtil.empty();
        }
        final Set<Map.Entry<String, Object>> ruleSet = JSONUtil.parseObj(rules).entrySet();
        return CollStreamUtil.toList(ruleSet, rule -> {
            QueryRuleInfo queryRuleInfo = new QueryRuleInfo();
            final String[] keys = rule.getKey().split("__");
            queryRuleInfo.setName(keys[0]);
            queryRuleInfo.setVal(Convert.toStr(rule.getValue()));
            queryRuleInfo.setQueryType(keys[1]);
            return queryRuleInfo;
        });
    }

}
